import { createSlice } from '@reduxjs/toolkit'
import pitStatic from '../../img/2.jpg'

import axios from 'axios'

export const DetailSlice = createSlice({
    // 命名空间，在调用action的时候会默认的设置为action的前缀,保证唯一.不重名
    name: 'detail',
    initialState: {
        // sumPrice1是收入 sumPrice2是支出
        // billType如果0只支出，1是收入
        userDetail: [
            {
                time: '2022年xx月xx日/周三',
                sumPrice1: '60.00',
                sumPrice2: '60.00',
                key: '001',
                value: [{
                    keyChildren: '001-1',
                    type: ['消费类型1', '消费类型1'],
                    avatar: pitStatic,
                    name: 'A',
                    remark: 'Deserunt dolor ea eaque eos',
                    extra: '-60.00',
                }, {
                    keyChildren: '001-2',
                    type: ['消费类型2', '消费类型2'],
                    avatar:
                        'https://images.unsplash.com/photo-1548532928-b34e3be62fc6?ixlib=rb-1.2.1&q=80&fm=jpg&crop=faces&fit=crop&h=200&w=200&ixid=eyJhcHBfaWQiOjE3Nzg0fQ',
                    name: 'B',
                    remark: 'Deserunt dolor ea eaque eos',
                    extra: '+60.00',
                }]
            },
            {
                time: '2022年xx月xx日/周二',
                sumPrice1: '60.00',
                sumPrice2: '60.00',
                key: '002',
                value: [{
                    keyChildren: '002-1',
                    type: ['消费类型1', '消费类型1'],
                    avatar:
                        'https://images.unsplash.com/photo-1548532928-b34e3be62fc6?ixlib=rb-1.2.1&q=80&fm=jpg&crop=faces&fit=crop&h=200&w=200&ixid=eyJhcHBfaWQiOjE3Nzg0fQ',
                    name: 'A',
                    remark: 'Deserunt dolor ea eaque eos',
                    extra: '+60.00',
                }, {
                    keyChildren: '002-2',
                    type: ['消费类型2', '消费类型2'],
                    avatar:
                        'https://images.unsplash.com/photo-1548532928-b34e3be62fc6?ixlib=rb-1.2.1&q=80&fm=jpg&crop=faces&fit=crop&h=200&w=200&ixid=eyJhcHBfaWQiOjE3Nzg0fQ',
                    name: 'B',
                    remark: 'Deserunt dolor ea eaque eos',
                    extra: '+60.00',
                }]
            },
            {
                time: '2021年xx月xx日/周三',
                sumPrice1: '60.00',
                sumPrice2: '60.00',
                key: '003',
                value: [{
                    keyChildren: '003-1',
                    type: ['消费类型1', '消费类型1'],
                    // 图片地址
                    avatar:
                        'https://images.unsplash.com/photo-1548532928-b34e3be62fc6?ixlib=rb-1.2.1&q=80&fm=jpg&crop=faces&fit=crop&h=200&w=200&ixid=eyJhcHBfaWQiOjE3Nzg0fQ',
                    // 商家名字
                    name: 'A',
                    // 备注
                    remark: 'Deserunt dolor ea eaque eos',
                    extra: '-60.00',
                }, {
                    keyChildren: '003-2',
                    type: ['消费类型3', '消费类型3'],
                    avatar:
                        'https://images.unsplash.com/photo-1548532928-b34e3be62fc6?ixlib=rb-1.2.1&q=80&fm=jpg&crop=faces&fit=crop&h=200&w=200&ixid=eyJhcHBfaWQiOjE3Nzg0fQ',
                    name: 'B',
                    remark: 'Deserunt dolor ea eaque eos',
                    extra: '+60.00',
                }]
            },
        ],
        typeDetail: [
            {
                // 展示都文字
                label: '食品酒水',
                // 获取的内容
                value: '食品酒水',
                children: [
                    {
                        label: '早午晚餐',
                        value: '早午晚餐',
                        mark: true,
                    },
                    {
                        label: '烟酒茶',
                        value: '烟酒茶',
                        mark: true,
                    }
                ],
            },
            {
                // 展示都文字
                label: '行车交通',
                // 获取的内容
                value: '行车交通',
                children: [
                    {
                        label: '公共交通',
                        value: '公共交通',
                        mark: true,
                    },
                    {
                        label: '私家车费用',
                        value: '私家车费用',
                        mark: true,
                    }
                ],
            },
            {
                // 展示都文字
                label: '居家物业',
                // 获取的内容
                value: '居家物业',
                children: [
                    {
                        label: '日常用品',
                        value: '日常用品',
                        mark: true,
                    },
                    {
                        label: '水电煤气',
                        value: '水电煤气',
                        mark: true,
                    },
                    {
                        label: '房租',
                        value: '房租',
                        mark: true,
                    },
                    {
                        label: '物业管理',
                        value: '物业管理',
                        mark: true,
                    },
                    {
                        label: '维修保养',
                        value: '维修保养',
                        mark: true,
                    },
                ],
            }],
        userDetail2: [
            {
                id: '1',
                time: '2022年1月xx日/周三',
                money: 60.00,
                type: ['食品酒水', '早午晚餐'],
                billType: 1,
                remark: 'Deserunt dolor ea eaque eos',
                avatar: pitStatic,
                name: 'A',
                billId:'1',
            },
            {
                id: '2',
                time: '2022年2月xx日/周二',
                money: 80.00,
                type: ['居家物业', '日常用品'],
                billType: 1,
                remark: 'Deserunt dolor ea eaque eos',
                avatar: pitStatic,
                name: 'B',
                billId: '2',
            },
            {
                id: '3',
                time: '2021年1月xx日/周五',
                money: 60.00,
                type: ['居家物业', '房租'],
                billType: 1,
                remark: 'aaaaa',
                avatar: pitStatic,
                name: 'A',
                billId: '3',
            },
            {
                id: '4',
                time: '2022年3月xx日/周四',
                money: 80.00,
                type: ['食品酒水', '早午晚餐'],
                billType: 1,
                remark: 'bbbb',
                avatar: pitStatic,
                name: 'A',
                billId: '4',
            },
            {
                id: '6',
                time: '2022年4月xx日/周三',
                money: 60.00,
                type: ['食品酒水', '早午晚餐'],
                billType: 1,
                remark: 'Deserunt dolor ea eaque eos',
                avatar: pitStatic,
                name: 'A',
                billId: '6',
            },
        ]
    },
    reducers: {
        // 定义的action-赋值的方式进行数据的改变

        // 添加数据
        userDetailAdd: (state, action) => {
            // console.log('action.payload', action.payload);
            state.userDetail2.push(action.payload)
        },
        // 删除数据
        DetailDelete: (state, action) => {
            state.userDetail2=state.userDetail2.filter(item=>item.id!==action.payload)
        },
        // 修改数据
        userDetailAlter: (state, action) => {
            state.userDetail2 = state.userDetail2.map(item => {
                if (item.id === action.payload.id) {
                    item = action.payload
                }
                return item
            })
        },
        // 类型状态改变
        typeDetailChange: (state, action) => {
            state.typeDetail = action.payload
        },
        // 获取数据
        DetailGet: (state, action) => {
            // console.log('dispatch成功');
            state.userDetail2 = action.payload
        },
    },
})

// 导出action
export const { userDetailAdd, DetailDelete, userDetailAlter, typeDetailChange, DetailGet } = DetailSlice.actions

// 异步数据处理
export const DetailGetAsync = (amount) => (dispatch) => {
    setTimeout(() => {
        dispatch(DetailGet(amount))
    }, 0)
}

// export const selectCount = (state) => state.counter.value

// 导出reducer
const DetailSliceReducer = DetailSlice.reducer
export default DetailSliceReducer